package com.hadron.media.slideshow.patterns.mvc.view 
{
	import com.hadron.media.slideshow.enum.SlideshowStatus;
	import com.hadron.media.slideshow.events.SlideshowEvent;
	import com.hadron.media.slideshow.events.SlideshowModelEvent;
	import com.hadron.media.slideshow.ISlideshow;
	import com.hadron.media.slideshow.items.ISlideshowItem;
	import com.hadron.media.slideshow.patterns.mvc.model.ISlideshowModel;
	import com.hadron.transitions.events.TransitionEvent;
	import flash.display.DisplayObject;
	import temple.core.CoreSprite;
	import temple.utils.types.ObjectUtils;
	

	/**
	 * SlideshowView
	 * @author Hadron
	 * date created: 05-22-2011
	 */
	public class SlideshowView extends CoreSprite 
	{
		///////////////////////////
		// FIELDS
		///////////////////////////
		/** @private */
		private var _imageContainer:CoreSprite;
		private var _model:ISlideshowModel;
		/**
		  * This method constructs instance of this class
		 */
		public function SlideshowView(model:ISlideshowModel) 
		{
			super();
			this._model = model;
			_imageContainer = new CoreSprite();
			addChild(_imageContainer);
			init();
		}
		
		/*------------------- PUBLIC METHODS -------------------*/
		public function clear():void
		{
			//remove listeners from currentItem
			if (_model.currentItem && _model.currentItem.hasEventListener(TransitionEvent.TRANSITION_IN_COMPLETE)) {
				_model.currentItem.removeEventListener(TransitionEvent.TRANSITION_IN_COMPLETE,
						handleItemTransitionInComplete);
			}
			
			//clear image container
			while (_imageContainer.numChildren > 0) {
				_imageContainer.removeChildAt(0);
			}
		}
		
		/*------------------ GETTERS & SETTERS -----------------*/
		
		
		/*------------------- PRIVATE METHODS ------------------*/
		private function init():void 
		{
			_model.addEventListener(SlideshowModelEvent.BACKGROUND_CHANGE, setBackground);
			_model.addEventListener(SlideshowModelEvent.ITEM_CHANGE, handleItemChange);
			_model.addEventListener(SlideshowModelEvent.ITEMS_LOAD_COMPLETE, handleImagesLoadComplete);
		}
		
		private function clearPrevItem():void
		{
			if (_imageContainer.numChildren > 1) {
				ISlideshowItem(_imageContainer.getChildAt(0)).transitionOut();
				_imageContainer.removeChildAt(0);
			}
		}
		
		private function showCurrentItem(disableItemTransition:Boolean = false):ISlideshowItem
		{			
			var item:ISlideshowItem = _model.currentItem;
			if (disableItemTransition) 
				item.transitionBehavior.disable();
			
			_imageContainer.addChild(item as DisplayObject);
			item.transitionIn();
			item.addEventListener(TransitionEvent.TRANSITION_IN_COMPLETE, handleItemAnimIn);
			return item as ISlideshowItem;
		}
		
		private function setBackground(event:SlideshowModelEvent = null):void 
		{
			this.graphics.clear();
			if (_model.background)
			{
				if (_model.debug)
					logDebug("redrawing background");
				this.graphics.beginFill(_model.backgroundColor, _model.backgroundAlpha);
				this.graphics.drawRect(0, 0, this.width, this.height);
				this.graphics.endFill();
			}
		}
		
		/*------------------- EVENT HANDLERS --------------------*/
		private function handleItemAnimIn(e:TransitionEvent):void 
		{
			_model.resetDisplayTimer();
		}
		
		private function handleImagesLoadComplete(e:SlideshowModelEvent):void 
		{
			if(_model.debug) logDebug('items load complete');
			if (_model.autoPlay) 
				_model.currentIndex = 0;
		}
		
		private function handleItemChange(e:SlideshowModelEvent):void 
		{
			if(_model.debug) logDebug('item change');
			showCurrentItem();
		}
		
		private function handleItemTransitionInComplete(e:TransitionEvent):void 
		{
			//dispatchEvent(new SlideshowEvent(SlideshowEvent.ITEM_IN_COMPLETE));
			ISlideshowItem(e.target).removeEventListener(TransitionEvent.TRANSITION_IN_COMPLETE, handleItemTransitionInComplete);
			if (_model.debug) logDebug('transitionIn completed - starting displayTime timer');
			if (_model.status == SlideshowStatus.PLAYING) 
				_model.resetDisplayTimer();
		}
	}
}